package com.isoftstone.merchant.controller.echarts;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.common.Page;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.bind.MBind;
import com.isoftstone.merchant.entity.echarts.Series;
import com.isoftstone.merchant.entity.feeUser.MFeeUser;
import com.isoftstone.merchant.entity.payment.Payment;
import com.isoftstone.merchant.service.bind.BindService;
import com.isoftstone.merchant.service.feeUser.FeeUserService;
import com.isoftstone.merchant.service.payment.PaymentService;
import com.isoftstone.util.Const;
import com.isoftstone.util.DateUtil;
import com.isoftstone.util.PageData;
import com.isoftstone.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by as on 2017/12/19.
 * 首页折线图累计
 */
@Controller
public class EchartsControlleradd extends BaseController{
    @Autowired
    FeeUserService feeUserService;
    @Autowired
    BindService bindService;
    @Autowired
    PaymentService paymentService;
    @RequestMapping("/merchant/eCharts/geteCharts")
    @ResponseBody
    public Object echartsDate(){
        PageData pd = this.getPageData();
        Page page=new Page();
        Tidings<Map<String,Object>>tidings=new Tidings< Map<String,Object>>();
        String userId=this.getUser().getUserid();
        String corpId=this.getUser().getCorpId();
        String lately=pd.getString("lately");//按照30,15,7天
        String dates=this.getRequest().getParameter("dates");//按照时间
        Map<String,Object>map=new HashMap<String,Object>();
        Integer day=30;
        Date daate=null;
        Date daate1=null;
        Date daate2=null;
        if(StringUtils.isNotBlank(lately)||day!=null){
            if(StringUtils.isBlank(dates)){
               daate= DateUtil.fomatDate(DateUtil.getDay());
               daate1=DateUtil.fomatDate(DateUtil.getDay());
               daate2=DateUtil.fomatDate(DateUtil.getDay());
            }else{
                String str[]=dates.split(",");
                day=days(str[1],str[0]);
                daate=DateUtil.fomatDate(str[1]);
                daate1=DateUtil.fomatDate(str[1]);
                daate2=DateUtil.fomatDate(str[1]);
                if(day==0){
                    day=1;
                }
            }
            if("30".equals(lately)){
                day=30;
            }else if("7".equals(lately)){
                day=7;
            }else if("15".equals(lately)){
                day=15;
            }
            List<String>list=new ArrayList<String>();
            List<String>date=new ArrayList<String>();
           List<Series>serieslist=new ArrayList<Series>();
            try {
                Series series=new Series();
                for(int i=0;i<day;i++){
                    series.setName("累计缴费用户数");
                    series.setType("line");
                    pd.put("corpId",corpId);
                    pd.put("endDate",daate);
                    pd.put("status","1");//生效的用户
                    page.setPd(pd);
                    MFeeUser mFeeUser=feeUserService.getCountFeeUser(page);
                    list.add(mFeeUser.getNumber());
                    date.add(DateUtil.dateToDateStr(daate));
                    daate=getNextDay(daate);
                }
                List<String>list1=new ArrayList<String>();
                List<Series>serieslist1=new ArrayList<Series>();
                Series series1=new Series();

                for(int i=0;i<day;i++){
                    series1.setName("累计签约用户数");
                    series1.setType("line");
                    pd.put("corpId",corpId);
                    pd.put("endDate",daate1);
                    pd.put("status","1");//解约的不算
                    page.setPd(pd);
                    MBind mBind=bindService.getBindUserNumber(page);
                    list1.add(mBind.getNumber());
                    daate1=getNextDay(daate1);
                }
                List<String>list2=new ArrayList<String>();
                List<Series>serieslist2=new ArrayList<Series>();
                Series series2=new Series();

                for(int i=0;i<day;i++){
                    series2.setName("累计交易总数");
                    series2.setType("line");
                    pd.put("corpId",corpId);
                    pd.put("endDate",daate2);
                    pd.put("payStatus","P-00");
                    page.setPd(pd);
                    Payment payment=paymentService.getPayUserNumber(page);
                    list2.add(payment.getNumber());
                    daate2=getNextDay(daate2);
                }
                Collections.reverse(list);
                series.setData(list);
                Collections.reverse(list1);
                series1.setData(list1);
                Collections.reverse(list2);
                series2.setData(list2);

                serieslist.add(series);
                serieslist.add(series1);
                serieslist.add(series2);

                Collections.reverse(date);
                map.put("series",serieslist);
                map.put("date",date);
                tidings.setStatus(Const.SUCCESS);
                tidings.setT(map);
            } catch (Exception e) {
                e.printStackTrace();
                tidings.setStatus(Const.SUCCESS);
                tidings.setMsg("数据异常");
            }
        }
        return tidings;
    }
    //当前时间减一天
    private  Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);//-1今天的时间加减天
        date = calendar.getTime();
        return date;
    }
    //算两个日期间隔多少天
    private int days(String day1,String day2)  {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = format.parse(day1);
            date2 = format.parse(day2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int a = (int) ((date1.getTime() - date2.getTime()) / (1000*3600*24))+1;
        return a;
    }
}
